package cn.hfatec.shms.web.xm.xmCgmx.service;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.HashMap;


import cn.hfatec.JsonResponse;
import cn.hfatec.shms.advice.annotation.MysqlDataSource;
import cn.hfatec.shms.cache.CodeInfoCache;
import cn.hfatec.shms.constants.ShmsConstant;
import cn.hfatec.shms.constants.ShmsProperties;
import cn.hfatec.shms.enums.CodeType;
import cn.hfatec.shms.exception.NotAuthException;
import cn.hfatec.shms.system.codeInfo.model.CodeInfo;
import cn.hfatec.shms.system.user.model.User;
import cn.hfatec.shms.utils.ExcelUtil;
import cn.hfatec.shms.web.xc.xcGsgj.model.GsgjCheck;
import cn.hfatec.shms.web.xc.xcGsgj.model.XcGsgj;
import cn.hfatec.shms.web.xm.xmCgmx.model.CgmxCheck;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hfatec.base.BaseDao;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.shms.web.xm.xmCgmx.model.XmCgmx;
import cn.hfatec.shms.web.xm.xmCgmx.dao.XmCgmxDao;
import cn.hfatec.shms.web.xm.xmCgmx.model.XmCgmxParams;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author hfatec
 * @version 1.0
 *          <p/>
 *          the JAVA code is generate by middlegen
 */
@Service("XmCgmxService")
public class XmCgmxService {

    public static final String BEAN_ID = "XmCgmxService";

    //注入Dao层
    @Resource(name = "XmCgmxDao")
    private XmCgmxDao xmCgmxDao;

    //注入BaseDao
    @Resource(name = "BaseDao")
    private BaseDao baseDao;
    @Resource(name = "shmsProperties")
    private ShmsProperties shmsProperties;

    /**
     * 查询数据
     *
     * @param xmCgmxParams
     * @param page
     */
    @Transactional
    public Table queryXmCgmxTable(XmCgmxParams xmCgmxParams, Page page) {

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", xmCgmxParams);
        return baseDao.queryTableByPage("queryXmCgmx", params, page);
    }


    /**
     * 查询数据
     *
     * @param xmCgmxParams
     * @param page
     */
    @Transactional
    public Table queryXmCgmxTableCf(XmCgmxParams xmCgmxParams, Page page) {

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", xmCgmxParams);
        return baseDao.queryTableByPage("queryXmCgmxCf", params, page);
    }
    /**
     * 查询数据
     *
     * @param xmCgmxParams

     */
    @Transactional
    public XmCgmx xmCgmxHJ(XmCgmxParams xmCgmxParams) {
        return xmCgmxDao.queryXmCgmxHj(xmCgmxParams);
    }

    /**
     * 插入数据
     *
     * @param xmCgmx
     */
    @Transactional
    public void saveXmCgmx(XmCgmx xmCgmx) {
        if (xmCgmx.getId() != null) {
            XmCgmx result = getXmCgmx(xmCgmx.getId());
            if (result != null) {
                xmCgmxDao.update(xmCgmx);
            } else {
                xmCgmxDao.insert(xmCgmx);
            }
        } else {
            xmCgmxDao.insert(xmCgmx);
        }
    }

    /**
     * 删除数据
     *
     * @param id
     */
    @Transactional
    public void deleteXmCgmx(Integer id) {

        xmCgmxDao.delete(id);
    }

    /**
     * 删除数据
     *
     * @param billTime
     */
    @Transactional
    public void deleteXmCgmxTime(String billTime) {

        xmCgmxDao.deleteTime( billTime);
    }

    /**
     * 删除数据
     *
     * @param xmCgmxList
     */
    @Transactional
    public void deleteXmCgmxBatch(List<String> xmCgmxList) {
    /*    for (String id : xmCgmxList) {
            deleteXmCgmx(Integer.valueOf(id));
        }*/
        for (String billTime : xmCgmxList) {
            deleteXmCgmxTime(billTime);
        }
    }

    /**
     * 根据ID获取数据
     *
     * @param id
     */
    @Transactional
    public XmCgmx getXmCgmx(Integer id) {

        return xmCgmxDao.getXmCgmxById(id);
    }

    /**
     * @param path
     * @param request
     * @param response
     * @return
     * @throws java.io.IOException
     */
    @Transactional
    @MysqlDataSource
    public String importCust(String path, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String filePath = path.substring(path.indexOf("/upload") + 7);
        User user = (User) request.getSession().getAttribute(ShmsConstant.LOGIN_USER_SESSION_NAME);
        if (user == null) {
            throw new NotAuthException("用户未授权");
        }
        String uploadPath = shmsProperties.getUploadPath();
        File file = new File(uploadPath + File.separator + filePath);
        boolean flag = false;
        String rule = "^1[358]\\d{9}$";
        String errorMsg = "";
        Workbook workbook = null;
        workbook = WorkbookFactory.create(file);
        Sheet sh = workbook.getSheetAt(0);//
        List<CgmxCheck> potentialCusts = this.buildCustWithAjaxUpload(sh);//
        if (potentialCusts != null && potentialCusts.size() > 0) {
            int i = 0;
            String bl = null;
            String[] riqi = new String[14];
            String[] content = new String[14];
            for (CgmxCheck cgmxCheck : potentialCusts) {
                //判读是否为有效数据
                if (cgmxCheck.isValid()) {
                    XmCgmx xmCgmx = cgmxCheck.getXmCgmx();
                    if (i == 0) {
                        bl = xmCgmx.getSpareH();
                    } else if (i == 1) {
                        riqi[0] = xmCgmx.getAreaA();
                        riqi[1] = xmCgmx.getAreaB();
                        riqi[2] = xmCgmx.getAreaC();
                        riqi[3] = xmCgmx.getAreaD();
                        riqi[4] = xmCgmx.getAreaE();
                        riqi[5] = xmCgmx.getAreaF();
                        riqi[6] = xmCgmx.getAreaG();
                        riqi[7] = xmCgmx.getHighA();
                        riqi[8] = xmCgmx.getHighB();
                        riqi[9] = xmCgmx.getHighC();
                        riqi[10] = xmCgmx.getHighD();
                        riqi[11] = xmCgmx.getHighE();
                        riqi[12] = xmCgmx.getHighF();
                        riqi[13] = xmCgmx.getHighG();
                    } else {
                        content[0] = xmCgmx.getAreaA();
                        content[1] = xmCgmx.getAreaB();
                        content[2] = xmCgmx.getAreaC();
                        content[3] = xmCgmx.getAreaD();
                        content[4] = xmCgmx.getAreaE();
                        content[5] = xmCgmx.getAreaF();
                        content[6] = xmCgmx.getAreaG();
                        content[7] = xmCgmx.getHighA();
                        content[8] = xmCgmx.getHighB();
                        content[9] = xmCgmx.getHighC();
                        content[10] = xmCgmx.getHighD();
                        content[11] = xmCgmx.getHighE();
                        content[12] = xmCgmx.getHighF();
                        content[13] = xmCgmx.getHighG();
                        XmCgmx xmCgmxAdd = new XmCgmx();
                        if (bl.equals("健管与体适能")) {
                            xmCgmxAdd.setZType(xmCgmx.getZType());
                            xmCgmxAdd.setXType(xmCgmx.getXType());
                            for (int j = 0; j < 14; j++) {
                                xmCgmxAdd.setBillTime(riqi[j]);
                                xmCgmxAdd.setSpareA(content[j]);
                                xmCgmxAdd.setSpareB("健管与体适能");
                                if (j <= 6)
                                    xmCgmxAdd.setDType("VIP");
                                else
                                    xmCgmxAdd.setDType("一区");
                                if (xmCgmxAdd.getSpareA() != null && xmCgmxAdd.getSpareA().length() > 0)
                                    saveXmCgmx(xmCgmxAdd);
                            }
                        } else if (bl.equals("来捡量")) {

                            xmCgmxAdd.setXType(xmCgmx.getXType());
                            for (int j = 0; j < 14; j++) {
                                xmCgmxAdd.setBillTime(riqi[j]);
                                xmCgmxAdd.setSpareA(content[j]);
                                xmCgmxAdd.setSpareB("来捡量");
                                if (j <= 6)
                                    xmCgmxAdd.setDType("一区");
                                else
                                    xmCgmxAdd.setDType("高阶");
                                if (xmCgmxAdd.getSpareA() != null && xmCgmxAdd.getSpareA().length() > 0)
                                    saveXmCgmx(xmCgmxAdd);
                            }
                        } else if (bl.equals("预约量")) {
                            xmCgmxAdd.setDType(xmCgmx.getDType());
                            xmCgmxAdd.setXType(xmCgmx.getXType());
                            xmCgmxAdd.setSpareB("预约量");
                            for (int j = 0; j < 7; j++) {
                                xmCgmxAdd.setBillTime(riqi[j]);
                                xmCgmxAdd.setSpareA(content[j]);
                                if (xmCgmxAdd.getSpareA() != null && xmCgmxAdd.getSpareA().length() > 0)
                                    saveXmCgmx(xmCgmxAdd);
                            }
                        }


                    }
                    i++;
                    flag = true;
                } else {//无效数据
                    errorMsg += "第" + cgmxCheck.getRowNumber() + "行的姓名或录入日期为空，不能导入!";
                }
            }
        } else {
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, "导入的文件无符合数据").toString();
        }

        if (flag) {
            if ("".equals(errorMsg)) {
                return new JsonResponse<String>(JsonResponse.RETURN_SUCCESS, "导入成功!" + errorMsg).toString();
            }
            return new JsonResponse<String>(JsonResponse.RETURN_SUCCESS, "部分导入成功!" + errorMsg).toString();
        } else {
            return new JsonResponse<String>(JsonResponse.RETURN_SUCCESS, "导入失败!" + errorMsg).toString();
        }

    }

    /**
     * 读取潜在客户导入的数据
     *
     * @param sh
     * @return
     * @throws Exception
     */
    @Transactional
    @MysqlDataSource
    private List<CgmxCheck> buildCustWithAjaxUpload(Sheet sh) throws Exception {
        List<CgmxCheck> checks = new ArrayList<CgmxCheck>();
        int rows = sh.getLastRowNum() + 1 - sh.getFirstRowNum(); // 总行数
        List<CodeInfo> sexList = CodeInfoCache.getCodeInfo(CodeType.SEX);
        ExcelUtil excelUtil = new ExcelUtil();
        for (int i = 0; i < rows; i++) {
            boolean flag = true;//数据是否有效(客户姓名，手机号存在)
            boolean flag2 = false;//判断是否该行有数据
            XmCgmx xmCgmx = new XmCgmx();
            CgmxCheck cgmxCheck = new CgmxCheck();
            cgmxCheck.setRowNumber(i + 1);
            cgmxCheck.setValid(true);
            String str = "";//下拉框
            // HSSFRow row = sh.getRow(i);//excel97-2003写法
            Row row = sh.getRow(i);
            Row first = sh.getRow(0);
//            int cols = row.getLastCellNum() + 1 - row.getFirstCellNum(); // 该行的总列数
            Iterator cells = row.cellIterator();
            int j = 0;
            String type = ExcelUtil.getStringRowCellValue(first, 0);
            while (cells.hasNext()) {

                if (type.equals("健管与体适能")) {
//                HSSFCell cell = (HSSFCell) cells.next();//excel97-2003写法
                    XSSFCell cell = (XSSFCell) cells.next();

                    if (j == 0) {
                        //卡号
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setZType(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 1) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setXType(ExcelUtil.getStringRowCellValue(row, j));
                        }

                    } else if (j == 2) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaA(ExcelUtil.getStringRowCellValue(row, j));
                        }

                    } else if (j == 3) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaB(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 4) {
                        // 年龄
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaC(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 5) {
                        // 年龄
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaD(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 6) {
                        // 年龄
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaE(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 7) {
                        // 年龄
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaF(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 8) {
                        // 年龄
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaG(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 9) {

                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setHighA(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 10) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setHighB(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 11) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setHighC(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 12) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setHighD(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 13) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setHighE(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 14) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setHighF(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 15) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setHighG(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    }
                } else if (type.equals("来捡量")) {
//                HSSFCell cell = (HSSFCell) cells.next();//excel97-2003写法
                    XSSFCell cell = (XSSFCell) cells.next();

                    if (j == 0) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setXType(ExcelUtil.getStringRowCellValue(row, j));
                        }

                    } else if (j == 1) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaA(ExcelUtil.getStringRowCellValue(row, j));
                        }

                    } else if (j == 2) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaB(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 3) {
                        // 年龄
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaC(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 4) {
                        // 年龄
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaD(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 5) {
                        // 年龄
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaE(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 6) {
                        // 年龄
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaF(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 7) {
                        // 年龄
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaG(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 8) {

                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setHighA(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 9) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setHighB(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 10) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setHighC(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 11) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setHighD(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 12) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setHighE(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 13) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setHighF(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 14) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setHighG(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    }
                } else if (type.equals("预约量")) {
//                HSSFCell cell = (HSSFCell) cells.next();//excel97-2003写法
                    XSSFCell cell = (XSSFCell) cells.next();

                    if (j == 0) {
                        //卡号
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setDType(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 1) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setXType(ExcelUtil.getStringRowCellValue(row, j));
                        }

                    } else if (j == 2) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaA(ExcelUtil.getStringRowCellValue(row, j));
                        }

                    } else if (j == 3) {
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaB(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 4) {
                        // 年龄
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaC(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 5) {
                        // 年龄
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaD(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 6) {
                        // 年龄
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaE(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 7) {
                        // 年龄
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaF(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    } else if (j == 8) {
                        // 年龄
                        if (!StringUtils.isEmpty(excelUtil.getStringRowCellValue(row, j))) {
                            xmCgmx.setAreaG(ExcelUtil.getStringRowCellValue(row, j));
                        }
                    }
                }
                xmCgmx.setSpareH(type);
                j++;
            }
            if (flag) {
//                 list.add(cust);
                cgmxCheck.setXmCgmx(xmCgmx);
                checks.add(cgmxCheck);
            } else {
//                vipCheck.setCustom(cust);
                if (flag2) {
                    checks.add(cgmxCheck);
                }
            }
        }
        return checks;
    }


}